<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ExternalContact;
use App\Models\FinanceCategory;
use App\Traits\Admin\SuccessOrFailure;
use Illuminate\Http\Request;

class FinanceCategoryController extends Controller
{
    use SuccessOrFailure;

    private $permission = 'common.finance_category';


    /**
     *  费用类型
     * @param Request $request
     * @return array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     * @Author: hql <858566651@qq.com>
     * @DateTime: 2021/12/18 16:01
     */
    public function index(Request $request)
    {
        $this->canOrFailure($this->permission);

        $categories = FinanceCategory::query()->orderBy('sort')->orderByDesc('id')->paginate();


        return view('admin.finance_category.index', compact('categories'));
    }


    /**
     * 添加联系人
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     * @Author: hql <858566651@qq.com>
     * @DateTime: 2021/12/18 16:07
     */
    public function create()
    {
        $this->canOrFailure($this->permission);
        return view('admin.finance_category.create');
    }

    public function store(Request $request)
    {
        $this->canOrFailure($this->permission);

        $attributes = $this->buildData($request);
        FinanceCategory::create($attributes);
        return $this->success('', route('admin.financeCategory.index'));
    }

    public function edit($id)
    {
        $this->canOrFailure($this->permission);
        $finance_category = $this->etFinanceCategoryOrFailure($id);
        return view('admin.finance_category.edit', compact('finance_category'));
    }

    public function update(Request $request, $id)
    {
        $this->canOrFailure($this->permission);

        $finance_category = $this->etFinanceCategoryOrFailure($id);
        $attributes = $this->buildData($request);

        $finance_category->update($attributes);

        return $this->success('', route('admin.financeCategory.index'));
    }

    public function buildData($request)
    {
        return [
            'name' => $request->input('name'),
            'has_sub' => $request->input('has_sub'),
            'is_cost' => $request->input('is_cost'),
            'is_external_cost' => $request->input('is_external_cost'),
            'enabled' => $request->input('enabled'),
            'sort' => intval($request->input('sort')),
        ];
    }

    public function destroy($id)
    {
        $this->canOrFailure($this->permission);

        $finance_category = $this->etFinanceCategoryOrFailure($id);
        $finance_category->delete();
        return $this->success();
    }

    private function etFinanceCategoryOrFailure($id)
    {
        $finance_category = FinanceCategory::query()->find($id);
        if (!$finance_category) {
            response($this->failure('值不存在！'), 404)->send();
            exit;
        }
        return $finance_category;
    }
}
